package server.servlet;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class WebContext {
       private List<Entity> entitys = null;
       private List<Mapping> mappings = null;

       //key用servlet-name   value用servlet-class
       private Map<String,String> entityMap = new HashMap<>();
    //key用servlet-pattern   value用servlet-name    如果用name当key会找到多个pattern
        private Map<String,String> mappingMap = new HashMap<>();
    public WebContext(List<Entity> entitys, List<Mapping> mappings) {
        this.entitys = entitys;
        this.mappings = mappings;

        //将entitys的List转成了map
        for (Entity entity:entitys
             ) {
            entityMap.put(entity.getName(),entity.getClz());
        }
        //将mappings的List转成了map
        for (Mapping mapping:mappings
                ) {
            for (String pattern:
            mapping.getPatterns()) {
                mappingMap.put(pattern,mapping.getName());
            }
        }
    }

    /**
     * 通过url的路径找到对应的class
     * @param pattern
     * @return
     */
    public String GetClz(String pattern){//根据pattern找是哪个实体类
        String name=mappingMap.get(pattern);
        return entityMap.get(name);
    }
}
